package com.lc.user.express.ordering;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.FurnitureAdapter;
import com.lc.user.express.httpserver.GetGoods;
import com.lc.user.express.model.FurnitureModel;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshGridView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/2.家具（某一类型）
 */
public class FurnitureChild2Activity extends BaseSecondActivity {
    private PullToRefreshGridView pgv;
    private FurnitureAdapter furnitureAdapter;
    private List<FurnitureModel> list = new ArrayList<>();
    private int ispage;
    private int nextpage;
    private GetGoods getGoods = new GetGoods("", 1, new AsyCallBack<GetGoods.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetGoods.Info info) throws Exception {
            if (type == 0) {
                list.clear();
            }
            list.addAll(info.dataList);
            furnitureAdapter.updateListView(list);
            ispage = info.ispage;
            nextpage = info.nextpage;
            pgv.onPullDownRefreshComplete();
            pgv.onPullUpRefreshComplete();
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_furniture_child2);


        setTitle(getIntent().getStringExtra("name"));


        pgv = (PullToRefreshGridView) findViewById(R.id.gv);


        getData(1, true, 0);
        pgv.setPullLoadEnabled(false);
        pgv.setScrollLoadEnabled(true);
        pgv.setPullRefreshEnabled(true);
        pgv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                getData(1, false, 0);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

                if (ispage == 1) {
                    getData(nextpage, false, 1);
                } else {
                    try {
                        pgv.onPullDownRefreshComplete();
                        pgv.onPullUpRefreshComplete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }

        });
        GridView gv = pgv.getRefreshableView();
        gv.setNumColumns(2);
        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        furnitureAdapter = new FurnitureAdapter(cnt, list, 3);
        gv.setAdapter(furnitureAdapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("title", list.get(position).getName());
                bundle.putString("goodsid", list.get(position).getId());
                show(FunitureNameActivity.class, bundle);
            }
        });
    }

    private void getData(int page, boolean isShow, int type) {
        getGoods.typeid = getIntent().getStringExtra("id");
        getGoods.page = page;
        getGoods.execute(cnt, isShow, type);
    }

}
